package chabernac.android.wordbattle.paint.crosstile;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;
import chabernac.android.wordbattle.WordBattleView;
import chabernac.android.wordbattle.model.Letter;
import chabernac.android.wordbattle.model.Word;
import chabernac.android.wordbattle.paint.iDrawable;

public class CharacterCrossTileDrawable extends iDrawable {

	private Letter myLetter;
	
	public CharacterCrossTileDrawable(
			Word aSelectedWord, 
			Letter aLetter) {
		myLetter = aLetter;
	}

	@Override
	public void paint(Canvas aCanvas, View aView) {
		WordBattleView theWordBattleView = (WordBattleView)aView;
	    int theX = WordBattleView.BORDER_SIZE + (int)(myLetter.getColumn() * theWordBattleView.getWidthPerColumn());
	    int theY = WordBattleView.BORDER_SIZE + (int)(myLetter.getRow() * theWordBattleView.getHeightPerRow());
	    
	    int theBorder = (int)theWordBattleView.getWidthPerColumn() / 20;
	    
	    RectF theRect = new RectF(theX + theBorder, theY + theBorder, theX + theBorder + theWordBattleView.getWidthPerColumn() - 2 * theBorder, theY + theBorder + theWordBattleView.getHeightPerRow() - 2 * theBorder);
	    
		float theMinSize = (float)(Math.min(theRect.width(), theRect.height()) ) * 0.61f;
		
		Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	    myPaint.setTextSize(theMinSize);
	    
	    Rect theFontBounds = new Rect();
	    myPaint.getTextBounds(myLetter.getLetter(), 0 , 1 , theFontBounds);
	    
	    float theFontWidth = theFontBounds.width();
	    float theFontHeight = theFontBounds.height();
	    
	    float theXOffset = (theWordBattleView.getWidthPerColumn() - theFontWidth) / 2F;
	    float theYOffset = (theWordBattleView.getWidthPerColumn() - theFontHeight) / 2F;
	    
	    float theXL = theX + theXOffset;
	    float theYL = theY +  theYOffset + theFontHeight; 
	    
	    myPaint.setShader(null);
	    myPaint.setStyle( Paint.Style.FILL );
	    myPaint.setColor(Color.BLACK);
	    myPaint.setFakeBoldText( true );
	    
	    aCanvas.drawText(myLetter.getLetter(), theXL - theFontBounds.left, theYL - theFontBounds.bottom , myPaint);

	}

}
